package com.zg.nacos.web;

import cn.hutool.core.util.StrUtil;
import com.zg.common.core.R;
import com.zg.nacos.domain.req.NodePageReq;
import com.zg.nacos.domain.req.RoleEditReq;
import com.zg.nacos.domain.req.RolePageReq;
import com.zg.nacos.domain.vo.NodeVo;
import com.zg.nacos.util.NacosUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.util.UriBuilder;
import reactor.core.publisher.Mono;

import javax.validation.Valid;
import java.time.Duration;
import java.util.List;
import java.util.Map;

/**
 * <p>Description: </p>
 *
 * @author zhanfeng.liang
 * @since 2025/6/20 16:11
 */

@RestController
@RequestMapping("/nacos")
@Api(tags = "nacos服务")
@Slf4j
public class NacosNodeController {



    @Autowired
    private WebClient webClient;

    @ApiOperation(value="节点列表")
    @PostMapping("/node/page")
    public R pageNode(@Valid  @RequestBody NodePageReq nodePageReq) {
        Map map= webClient.get()
                .uri(uriBuilder -> {
                    UriBuilder builder = uriBuilder.path("/nacos/v1/core/cluster/nodes");
                    // 仅当参数非空时才添加 queryParam
                    if (!StrUtil.isEmpty(nodePageReq.getKeyword())) {
                        builder.queryParam("keyword",nodePageReq.getKeyword());
                    }
                    builder.queryParam("pageSize", nodePageReq.getPageSize());
                    builder.queryParam("pageNo", nodePageReq.getPageNo());
                    builder.queryParam("namespaceId", nodePageReq.getKeyword());
                    builder.queryParam("accessToken",nodePageReq.getNacosToken());
                    return builder.build();
                })
                .retrieve()
                .bodyToMono(Map.class)
                .block(Duration.ofSeconds(5));
        log.info("map="+map);
        List<Map<String,Object>> list = (List)map.get("data");
        List<NodeVo> reList = Lists.newArrayList();
        for(int i=0;i<list.size();i++){
            NodeVo nodeVo =new NodeVo();
            Map<String,Object> map1 = list.get(i);
            nodeVo.setAddress((String)map1.get("address"));
            nodeVo.setState((String)map1.get("state"));
            reList.add(nodeVo);
        }
        return R.success(reList);
    }

}